/*
 * Galaxium Messenger
 *
 * Copyright (C) 2008 Philippe Durand <draekz@gmail.com>
 * Copyright (C) 2008 Ben Motmans <ben.motmans@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.Collections.Generic;

using Gtk;
using Glade;

using Galaxium.Core;
using Galaxium.Gui;
using Galaxium.Gui.GtkGui;
using Galaxium.Protocol;
using Galaxium.Protocol.Gui;

using Anculus.Core;

namespace Galaxium.Client.GtkGui
{
	public class ProxyPreferenceWidget : IPreferenceWidget<Widget>
	{
		[Widget("httpEnableCheck")] Gtk.CheckButton _httpEnableCheck;
		[Widget("httpHostEntry")] Gtk.Entry _httpHostEntry;
		[Widget("httpPortEntry")] Gtk.Entry _httpPortEntry;
		[Widget("httpUsernameEntry")] Gtk.Entry _httpUsernameEntry;
		[Widget("httpPasswordEntry")] Gtk.Entry _httpPasswordEntry;
		
		[Widget("httpsEnableCheck")] Gtk.CheckButton _httpsEnableCheck;
		[Widget("httpsHostEntry")] Gtk.Entry _httpsHostEntry;
		[Widget("httpsPortEntry")] Gtk.Entry _httpsPortEntry;
		[Widget("httpsUsernameEntry")] Gtk.Entry _httpsUsernameEntry;
		[Widget("httpsPasswordEntry")] Gtk.Entry _httpsPasswordEntry;
		
		[Widget("socksEnableCheck")] Gtk.CheckButton _socksEnableCheck;
		[Widget("socksHostEntry")] Gtk.Entry _socksHostEntry;
		[Widget("socksPortEntry")] Gtk.Entry _socksPortEntry;
		[Widget("socksUsernameEntry")] Gtk.Entry _socksUsernameEntry;
		[Widget("socksPasswordEntry")] Gtk.Entry _socksPasswordEntry;
		
		private Widget _nativeWidget;
		private Widget _parentLayoutWidget;
		private IConfigurationSection _config;
		
		public void Initialize ()
		{
			_nativeWidget = GladeUtility.ExtractWidget<Widget> (GladeUtility.GetGladeResourceStream (typeof (ProxyPreferenceWidget).Assembly, "ProxyPreferenceWidget.glade"), "widget", this);
			
			_config = Configuration.Proxy.Section;
			
			_nativeWidget.ShowAll ();
			
			_socksEnableCheck.Toggled += SocksEnableToggled;
			_httpEnableCheck.Toggled += HttpEnableToggled;
			_httpsEnableCheck.Toggled += HttpsEnableToggled;
			
			_socksHostEntry.LeaveNotifyEvent += SocksHostChanged;
			_socksPortEntry.LeaveNotifyEvent += SocksPortChanged;
			_socksUsernameEntry.LeaveNotifyEvent += SocksUsernameChanged;
			_socksPasswordEntry.LeaveNotifyEvent += SocksPasswordChanged;
			
			_httpsHostEntry.LeaveNotifyEvent += HttpsHostChanged;
			_httpsPortEntry.LeaveNotifyEvent += HttpsPortChanged;
			_httpsUsernameEntry.LeaveNotifyEvent += HttpsUsernameChanged;
			_httpsPasswordEntry.LeaveNotifyEvent += HttpsPasswordChanged;
			
			_httpHostEntry.LeaveNotifyEvent += HttpHostChanged;
			_httpPortEntry.LeaveNotifyEvent += HttpPortChanged;
			_httpUsernameEntry.LeaveNotifyEvent += HttpUsernameChanged;
			_httpPasswordEntry.LeaveNotifyEvent += HttpPasswordChanged;
			
			LoadSettings ();
		}

		public Widget NativeWidget
		{
			get { return _nativeWidget; }
		}
		
		private void SocksEnableToggled (object sender, EventArgs args)
		{
			_config.SetBool (Configuration.Proxy.UseSocksProxy.Name, _socksEnableCheck.Active);
			ChangeSocksState (_socksEnableCheck.Active);
		}
		
		private void HttpsEnableToggled (object sender, EventArgs args)
		{
			_config.SetBool (Configuration.Proxy.UseHttpsProxy.Name, _httpsEnableCheck.Active);
			ChangeHttpsState (_httpsEnableCheck.Active);
		}
		
		private void HttpEnableToggled (object sender, EventArgs args)
		{
			_config.SetBool (Configuration.Proxy.UseHttpProxy.Name, _httpEnableCheck.Active);
			ChangeHttpState (_httpEnableCheck.Active);
		}
		
		private void LoadSettings ()
		{
			_httpEnableCheck.Active = _config.GetBool (Configuration.Proxy.UseHttpProxy.Name, Configuration.Proxy.UseHttpProxy.Default);
			_httpsEnableCheck.Active = _config.GetBool (Configuration.Proxy.UseHttpsProxy.Name, Configuration.Proxy.UseHttpsProxy.Default);
			_socksEnableCheck.Active = _config.GetBool (Configuration.Proxy.UseSocksProxy.Name, Configuration.Proxy.UseSocksProxy.Default);
			
			_socksHostEntry.Text = _config.GetString (Configuration.Proxy.SocksHost.Name, Configuration.Proxy.SocksHost.Default);
			_socksPortEntry.Text = _config.GetInt (Configuration.Proxy.SocksPort.Name, Configuration.Proxy.SocksPort.Default).ToString ();
			_socksUsernameEntry.Text = _config.GetString (Configuration.Proxy.SocksUsername.Name, Configuration.Proxy.SocksUsername.Default);
			_socksPasswordEntry.Text = _config.GetString (Configuration.Proxy.SocksPassword.Name, Configuration.Proxy.SocksPassword.Default);
			
			_httpsHostEntry.Text = _config.GetString (Configuration.Proxy.HttpsHost.Name, Configuration.Proxy.HttpsHost.Default);
			_httpsPortEntry.Text = _config.GetInt (Configuration.Proxy.HttpsPort.Name, Configuration.Proxy.HttpsPort.Default).ToString ();
			_httpsUsernameEntry.Text = _config.GetString (Configuration.Proxy.HttpsUsername.Name, Configuration.Proxy.HttpsUsername.Default);
			_httpsPasswordEntry.Text = _config.GetString (Configuration.Proxy.HttpsPassword.Name, Configuration.Proxy.HttpsPassword.Default);
			
			_httpHostEntry.Text = _config.GetString (Configuration.Proxy.HttpHost.Name, Configuration.Proxy.HttpHost.Default);
			_httpPortEntry.Text = _config.GetInt (Configuration.Proxy.HttpPort.Name, Configuration.Proxy.HttpPort.Default).ToString ();
			_httpUsernameEntry.Text = _config.GetString (Configuration.Proxy.HttpUsername.Name, Configuration.Proxy.HttpUsername.Default);
			_httpPasswordEntry.Text = _config.GetString (Configuration.Proxy.HttpPassword.Name, Configuration.Proxy.HttpPassword.Default);
			
			ChangeHttpState (_httpEnableCheck.Active);
			ChangeHttpsState (_httpsEnableCheck.Active);
			ChangeSocksState (_socksEnableCheck.Active);
		}
		
		private void ChangeSocksState (bool enabled)
		{
			_socksHostEntry.Sensitive = enabled;
			_socksPortEntry.Sensitive = enabled;
			_socksUsernameEntry.Sensitive = enabled;
			_socksPasswordEntry.Sensitive = enabled;
		}
		
		private void ChangeHttpsState (bool enabled)
		{
			_httpsHostEntry.Sensitive = enabled;
			_httpsPortEntry.Sensitive = enabled;
			_httpsUsernameEntry.Sensitive = enabled;
			_httpsPasswordEntry.Sensitive = enabled;
		}
		
		private void ChangeHttpState (bool enabled)
		{
			_httpHostEntry.Sensitive = enabled;
			_httpPortEntry.Sensitive = enabled;
			_httpUsernameEntry.Sensitive = enabled;
			_httpPasswordEntry.Sensitive = enabled;
		}

		private void HttpsHostChanged (object sender, LeaveNotifyEventArgs args)
		{
			_config.SetString (Configuration.Proxy.HttpsHost.Name, _httpsHostEntry.Text);
		}
	
		private void HttpsPortChanged (object sender, LeaveNotifyEventArgs args)
		{
			int port = 0;
			
			if (int.TryParse (_httpsPortEntry.Text, out port))
				_config.SetInt (Configuration.Proxy.HttpsPort.Name, port);
			else
				Log.Warn ("Was unable to parse the port as a int: "+_httpsPortEntry.Text);
		}
	
		private void HttpsUsernameChanged (object sender, LeaveNotifyEventArgs args)
		{
			_config.SetString (Configuration.Proxy.HttpsUsername.Name, _httpsUsernameEntry.Text);
		}
	
		private void HttpsPasswordChanged (object sender, LeaveNotifyEventArgs args)
		{
			_config.SetString (Configuration.Proxy.HttpsPassword.Name, _httpsPasswordEntry.Text);
		}
		
		private void HttpHostChanged (object sender, LeaveNotifyEventArgs args)
		{
			_config.SetString (Configuration.Proxy.HttpHost.Name, _httpHostEntry.Text);
		}
	
		private void HttpPortChanged (object sender, LeaveNotifyEventArgs args)
		{
			int port = 0;
			if (int.TryParse (_httpPortEntry.Text, out port))
				_config.SetInt (Configuration.Proxy.HttpPort.Name, port);
			else
				Log.Warn ("Was unable to parse the port as a int: "+_httpPortEntry.Text);
		}
	
		private void HttpUsernameChanged (object sender, LeaveNotifyEventArgs args)
		{
			_config.SetString (Configuration.Proxy.HttpUsername.Name, _httpUsernameEntry.Text);
		}
	
		private void HttpPasswordChanged (object sender, LeaveNotifyEventArgs args)
		{
			_config.SetString (Configuration.Proxy.HttpPassword.Name, _httpPasswordEntry.Text);
		}
		
		private void SocksHostChanged (object sender, LeaveNotifyEventArgs args)
		{
			_config.SetString (Configuration.Proxy.SocksHost.Name, _socksHostEntry.Text);
		}
	
		private void SocksPortChanged (object sender, LeaveNotifyEventArgs args)
		{
			int port = 0;
			if (int.TryParse (_socksPortEntry.Text, out port))
				_config.SetInt (Configuration.Proxy.SocksPort.Name, port);
			else
				Log.Warn ("Was unable to parse the port as a int: "+_socksPortEntry.Text);
		}
	
		private void SocksUsernameChanged (object sender, LeaveNotifyEventArgs args)
		{
			_config.SetString (Configuration.Proxy.SocksUsername.Name, _socksUsernameEntry.Text);
		}
	
		private void SocksPasswordChanged (object sender, LeaveNotifyEventArgs args)
		{
			_config.SetString (Configuration.Proxy.SocksPassword.Name, _socksPasswordEntry.Text);
		}
	}
}